import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;

import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;

import android.widget.TextView;
import android.widget.Toast;

public class MediaPlayerDemo_Audio extends Activity implements SeekBar.OnSeekBarChangeListener{

    private static final String TAG = "MediaPlayerDemo";
    private MediaPlayer mMediaPlayer;
    private Button audioplay;
    private Button audiopause;
    private Button audioreset;//记住下面的几个按钮必须在相应的xml文件添加
    private Button audiolooping;
    private Button audioseekplay; 
    private Button audiostop;
    private Button audioupplay;
    private Button audionextplay;
    //播放音乐情况文本显示
    TextView mProgressText;
    TextView mTrackingText;
    //进度条定义
    SeekBar mSeekBar;
    //进度条运行记录
    int progress_run=0;
    boolean progress_run_boolean=true;

    private ProgressBar mProgress;//进度条的控制
    private int mProgressStatus = 0;//
    private Handler mHandler = new Handler();//
    //下面用来控制进度条
    boolean running=true;//控制进度条的活动
   
    //下面的这个变量lasttime用来控制复读进度位置
    private int lasttime;
    //下面的参数是用来进行音频播放的读取的
    int audiolenth;

    private static final String MEDIA = "media";
    private static final int LOCAL_AUDIO = 1;
    private static final int STREAM_AUDIO = 2;
    private static final int RESOURCES_AUDIO = 3;
    private static final int LOCAL_VIDEO = 4;
    private static final int STREAM_VIDEO = 5;
    private String path;
    private TextView tx;
    int songnumber=1;
    private static final int songmax=5;
    int stop=0;
    //onCreate()的使用

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        tx = new TextView(this);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(tx);
        setContentView(R.layout.audio);
    
        //进度条，计时器控制

        mSeekBar = (SeekBar)findViewById(R.id.seek);
        mSeekBar.setOnSeekBarChangeListener(this);
        countdown();
        seekbarrun();       
   
        Bundle extras = getIntent().getExtras();
        //可以在此处增加代码实现播放的控制
        //playAudio(extras.getInt(MEDIA));
        playAudio(4);
        //进度条的生成

        mSeekBar = (SeekBar)findViewById(R.id.seek);
        //文本显示

        mProgressText = (TextView)findViewById(R.id.progress);
        mTrackingText = (TextView)findViewById(R.id.tracking);
        //各个按钮的操作响应函数
        audioplay=(Button) findViewById(R.id.audioplay);
        audioplay.setOnClickListener(audioplayListener);
        audiopause=(Button) findViewById(R.id.audiopause);
        audiopause.setOnClickListener(audiopauseListener);
        audioreset=(Button)findViewById(R.id.audioreset);
        audioreset.setOnClickListener(audioresetListener);
        audiolooping=(Button)findViewById(R.id.audiolooping);
        audiolooping.setOnClickListener(audioloopingListener);
        audioseekplay=(Button)findViewById(R.id.audioreplay);
        audioseekplay.setOnClickListener(audioreplaying);

        audiostop=(Button)findViewById(R.id.audiodestroy);
        audiostop.setOnClickListener(audiostopListener);
        audionextplay=(Button)findViewById(R.id.audionextplay);
        audionextplay.setOnClickListener(audionextplaying);
        audioupplay=(Button)findViewById(R.id.audioupplay);
        audioupplay.setOnClickListener(audioupplaying);  
    }
   
    private void countdown(){
        new CountDownTimer(1000000, 1000) {

         public void onTick(long millisUntilFinished) {
             mTrackingText.setText("seconds remaining: " + millisUntilFinished / 1000);
         }

         public void onFinish() {
             mTrackingText.setText("done!");
         }
      }.start();
    }
    private void seekbarrun(){

        new Thread(new Runnable(){
        public void run(){
            //为了让进程能够一直运行下去直到用户命令其终止时方终止，这可以使用while死循环来实现。
          for(int i=0;i<1000;i++){
                try{
                    Thread.sleep(1000);
                    if(progress_run_boolean==true)
                        progress_run++;
                }catch(InterruptedException e){
                    System.out.println("WrongQQQQQQQQQQQQQQQQQQQ");
                }
                mSeekBar.setProgress(progress_run);
            }
        }
        }).start();

    }
    //暂停播放
    private OnClickListener audiopauseListener=new OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub
            running=false;
            mMediaPlayer.pause();
            progress_run_boolean=false;
        }
    };
    //这个函数是用在对进度条进行动态控制中

   
    private OnClickListener audioplayListener=new OnClickListener(){
        public void onClick(View v){
            if(stop==1)
            {   
                stop=0;
                playAudio(1);
            }
            else{
                running=true;
                mMediaPlayer.start();
                progress_run_boolean=true;

            }
        }
    };
    private OnClickListener audioresetListener=new OnClickListener(){
        public void onClick(View v){
             Bundle extras = getIntent().getExtras();
            mMediaPlayer.reset();  
            progress_run_boolean=true;
            progress_run=0;

            playAudio(extras.getInt(MEDIA));   
        }
    };
    private OnClickListener audioloopingListener=new OnClickListener(){
        public void onClick(View v){
            //一班情况下，我们不可能永久的循环播放下去，而是让其播放一定的次数，那么我们需要进行
            //循环播放的次数 ，这样就需要添加一个变量来记录要循环播放的次数的值得。
            mMediaPlayer.setLooping(true);
        }
    };
    //这里要增加一个停止循环函数audioloopingstop()

    private OnClickListener audiostopListener=new OnClickListener(){
        public void onClick(View v){
            mMediaPlayer.stop();
            //同样的，当停止播放时，显示的时间值为0
            mMediaPlayer.release();
            stop=1;
        }
    };
 
   
    //这个函数用来实现复读功能，这是根据上次按控制条来进行复读的，变量lasttime即记录这个时刻。
    private OnClickListener audioreplaying=new OnClickListener(){

        public void onClick(View v){
           
            mMediaPlayer.seekTo(lasttime);   //此功能是用来根据进度条的位置来控制音频播放的       
        }
    };
    //
    private OnClickListener audionextplaying=new OnClickListener(){
        public void onClick(View v){
            mMediaPlayer.stop();
            mMediaPlayer.release();
            songnumber--;  //
            if(songnumber<=0)
                songnumber=songmax;
            playAudio(songnumber);
            progress_run=0;
            mSeekBar.setProgress(progress_run);

        }
    };
    private OnClickListener audioupplaying=new OnClickListener(){
        public void onClick(View v){
            mMediaPlayer.stop();
            mMediaPlayer.release();
            songnumber++;
            if(songnumber>=songmax)
                songnumber=1;
            playAudio(songnumber);
            progress_run=0;
            mSeekBar.setProgress(progress_run);
    }
    };   
- 隐藏引用文字 -
    private void progressdo(){
        //控制进度条线程
         new Thread(new Runnable() {
             public void run() {
                 while (mProgressStatus < 10000) {
                     if(running){
                         mProgress.incrementProgressBy(1);
                     }
                     try{
                     Thread.sleep(1000);
                     }catch(InterruptedException exc){
                     }
                     if(running){
                         mProgressStatus++;
                         
                     }
                     mHandler.post(new Runnable() {
                         public void run() {                          
                              // Title progress is in range 0..10000
                              setProgress(50* mProgress.getProgress());
                         }
                     });
                     
                 }
             }
         }).start();
    }
    private void playAudio(Integer media) {
        try {
            switch (media) {
                case 1:
                    path ="file:///home/huanghucai/My_zone/我的音乐/音乐/guangliang/thechampion";
                    if (path == "") {
                        // Tell the user to provide an audio file URL.
                         Toast
                         .makeText(
                                 MediaPlayerDemo_Audio.this,
                                 "Please edit MediaPlayerDemo_Video Activity, "
                                         + "and set the path variable to your media file path."
                                         + " Your media file must be stored on sdcard.",
                                 Toast.LENGTH_LONG).show();    }
                    File f=new File(path);
                   
                    //mMediaPlayer =new MediaPlayer();
                    //mMediaPlayer.setDataSource(path);
                    mMediaPlayer=MediaPlayer.create(this,R.raw.beatifulfable);//Uri.fromFile(f));//R.raw.beatifulfable);
            //        mMediaPlayer.prepare();
                    mMediaPlayer.start();
                   break;
                case 3:
                    /**
                     * TODO: Upload a audio file to res/raw folder and provide
                     * its resid in MediaPlayer.create() method.
                     */
                   mMediaPlayer = MediaPlayer.create(this, R.raw.beatifulfable);
                   mMediaPlayer.start();
                    break;
                case 4:
                    mMediaPlayer=MediaPlayer.create(this,R.raw.beatifulfable);
               //     mMediaPlayer.prepare();
                    mMediaPlayer.start();
                    break;
                case 5:
                    /**
                     * TODO: Upload a audio file to res/raw folder and provide
                     * its resid in MediaPlayer.create() method.
                     */
                    mMediaPlayer = MediaPlayer.create(this, R.raw.beatifulfable);
           //         mMediaPlayer.prepare();
                    mMediaPlayer.start();
                   progressdo();
                    break;
                case 2:
                    mMediaPlayer =MediaPlayer.create(this,R.raw.beatifulfable);
            //        mMediaPlayer.prepare();
                    mMediaPlayer.start();
                    break;
            }
            tx.setText("Playing audio...");

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // TODO Auto-generated method stub
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
    //////////////////////////////////////////////这里是对控制条的控制

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        mProgressText.setText(progress + " " +
                getString(R.string.seekbar_from_touch) + "=" + fromTouch);
        progress_run=progress;
        //mMediaPlayer.seekTo(progress_run);

    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText(getString(R.string.seekbar_tracking_on));
  //      mMediaPlayer.seekTo(progress_run);

    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText(getString(R.string.seekbar_tracking_off));
        mMediaPlayer.seekTo(progress_run*1000);//在使用这个函数时应该要十分注意的一点就是，这里的参数的单位
        //是毫秒，而不是秒，在进行控制的时候，容易产生错误，因为时间的转换发生了错误
    }
}
